﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using GKQC.DbHelper.Services;
using GKQC.Language;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GKQC.Ort
{
    /// <summary>
    /// 把检测单检测数据后半部分转移动另一张单上
    /// </summary>
    public partial class FormTranData : Form
    {
        public string SourceBillNo { get; set; }
        public int SourceSNo { get; set; }
        public string TargetBillNo { get; set; }
        public int TargetSNo { get; set; }
        OrtHeadModel _Head;

        UserModel _User; string _BillType; List<ReportSetModel> _ReportSetModels;
        OrtService _OrtService;

        public FormTranData(OrtHeadModel head , int sourceSNo
            ,UserModel user, string billType, List<ReportSetModel> reportSetModels
            , OrtService ortService
            )
        {
            InitializeComponent();
            this._Head = head;
            this._OrtService = ortService;
            if(_Head != null) this.txtSourceBillNo.Text = _Head.BillNo;
            this.numSourceSNo.Value = sourceSNo;
            this.TargetSNo = 0;
            this.TargetBillNo = string.Empty;
            this._User = user;
            this._BillType = billType;
            this._ReportSetModels = reportSetModels;

            LanguageHelper.SetControlLanguageText(this);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace(txtSourceBillNo.Text))
            {
                //来源单号
                QMessageBox.Show(lblSourceBillNo.Text + "不能为空"
                    , "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                return;
            }
            if (string.IsNullOrWhiteSpace(txtTargetBillNo.Text))
            {
                //目标单号
                QMessageBox.Show(lblTargetBillNo.Text + "目不能为空"
                    , "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                return;
            }

            if (txtSourceBillNo.Text == txtTargetBillNo.Text)
            {
                //来源单号与目标单号不能相同
                QMessageBox.Show(lblSourceBillNo.Text + "与"+ lblTargetBillNo.Text+ "不能相同"
                    , "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                return;
            }

            if (numSourceSNo.Value<=0)
            {
                //来源次数
                QMessageBox.Show(lblSourceSNo.Text + "必须大于0"
                    ,"提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                return;
            }
            if (numTargetSNo.Value <= 0)
            {
                //目标次数
                QMessageBox.Show(lblTargetSNo.Text + "必须大于0"
                    , "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.DialogResult = DialogResult.None;
                return;
            }

            //List<OrtDataModel> tartDatas = _OrtService.GetDatas(txtTargetBillNo.Text, (int)this.numTargetSNo.Value);
            //if (tartDatas == null || tartDatas.Count > 0)
            //{
            //    QMessageBox.Show("目标单号对应的测试次数已有数据，不能转移: " + txtTargetBillNo.Text
            //        , "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            //    this.DialogResult = DialogResult.None;
            //    return;
            //}

            SourceBillNo = txtSourceBillNo.Text;
            SourceSNo = (int)numSourceSNo.Value;
            TargetBillNo = txtTargetBillNo.Text;
            TargetSNo = (int)numTargetSNo.Value;

            //todo: 锁定
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                FormQuery f = new FormQuery(_User, _BillType, _ReportSetModels);
                if (sender == btnTargetQuery)
                    f.SetBillNo(txtSourceBillNo.Text);
                else if (sender == btnSourceQuery)
                    f.SetBillNo(txtTargetBillNo.Text);

                if (f.ShowDialog() == DialogResult.OK)
                {
                    if (sender == btnTargetQuery)
                    {
                        txtTargetBillNo.Text = f.BillNo;
                        numTargetSNo.Value = f.SNo;
                        TargetBillNo = f.BillNo;
                        TargetSNo = f.SNo;
                    }
                    else if(sender == btnSourceQuery)
                    {
                        txtSourceBillNo.Text = f.BillNo;
                        numSourceSNo.Value = f.SNo;
                        SourceBillNo = f.BillNo;
                        SourceSNo = f.SNo;
                    }
                }                
            }
            catch(Exception ex)
            {
                Logger.LogError(ex);
            }
        }


        /// <summary>
        /// 设置目标单据
        /// </summary>
        /// <param name="billNo"></param>
        /// <param name="sno"></param>
        public void SetTargetBill(string billNo, int sno)
        {
            txtTargetBillNo.Text = billNo.ToString();
            numTargetSNo.Value = sno;
        }

        public void SetTitle(string title)
        {
            this.Text = title;
        }

        public void SetDoubleTestLabel()
        {
            this.Text = LanguageHelper.GetLanguageText("检测单号和次数选择");
            this.lblSourceBillNo.Text = LanguageHelper.GetLanguageText("第一单号");
            this.lblTargetBillNo.Text = LanguageHelper.GetLanguageText("第二单号");
            this.lblSourceSNo.Text = LanguageHelper.GetLanguageText("第一次数");
            this.lblTargetSNo.Text = LanguageHelper.GetLanguageText("第二次数");
        }

        public void SetSourceBillNoLabelText(string text)
        {
            this.lblSourceBillNo.Text = text;
        }

        public void SetTargetBillNoLabelText(string text)
        {
            this.lblTargetBillNo.Text = text;
        }

        public void SetSourceSNoLabelText(string text)
        {
            this.lblSourceSNo.Text = text;
        }

        public void SetTargetSNoLabelText(string text)
        {
            lblTargetSNo.Text = text;
        }
    }
}
